﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Data;
using DevExpress.Web.ASPxGridView;


namespace WebApplication1.Admin
{
    public partial class AgentesListado : System.Web.UI.Page
    {

        #region AgentesDanzaDataTableCache
        private DataTable AgentesDanzaDataTableCache
        {
            get
            {
                DataTable objTable = null;
                if (Cache["AgentesListado.aspx" + Session.SessionID] != null)
                    objTable = (DataTable)Cache["AgentesListado.aspx" + Session.SessionID];
                return objTable;
            }
            set
            {
                if (value != null)
                {
                    Cache.Insert("AgentesListado.aspx" + Session.SessionID, value, null, DateTime.Now.AddMinutes(20), TimeSpan.Zero);
                }
            }
        }
        #endregion

        #region Page_Load
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {

                if (Session["USU_USUARIO"] == null)
                {
                    Response.Redirect("../login.aspx");
                    return;
                }

                AgentesDanzaDataTableCache = null;
                BindDatosAgentes();
            }
            else
            {
                if (AgentesDanzaDataTableCache != null)
                {
                    ASPxGridViewAgentes.DataSource = AgentesDanzaDataTableCache;
                    ASPxGridViewAgentes.DataBind();
                }
            }

        }
        #endregion

        #region StrAgeId
        public string StrAgeId
        {
            get
            {
                if (ViewState["StrAgeId"] != null)
                    return ViewState["StrAgeId"].ToString();
                return "";
            }
            set { ViewState["StrAgeId"] = value; }
        }
        #endregion

        #region crearContextItems
        private void crearContextItems()
        {
            try
            {
                Context.Items["StrAgeId"] = StrAgeId;
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg1_error", "alert('" + ex.Message.ToString() + "');", true);
            }
        }
        #endregion

        #region GetDataView
        private DataTable GetDataView(ASPxGridView grid)
        {
            DataTable dt = new DataTable();
            foreach (GridViewColumn col in grid.VisibleColumns)
            {
                GridViewDataColumn dataColumn = col as GridViewDataColumn;
                if (dataColumn == null) continue;
                dt.Columns.Add(dataColumn.FieldName);
            }
            for (int i = 0; i < grid.VisibleRowCount; i++)
            {
                DataRow row = dt.Rows.Add();
                foreach (DataColumn col in dt.Columns)
                    row[col.ColumnName] = grid.GetRowValues(i, col.ColumnName);
            }
            return dt;
        }
        #endregion

        #region BindDatosAgentes
        private void BindDatosAgentes()
        {
            try
            {
                Model.DataClassesAgentesDataContext contexto = new Model.DataClassesAgentesDataContext();
                var datos = from a in contexto.VISTA_LISTADO_AGENTES_SIDANZAs
                            //join d in contexto on a.ZON_ID equals d.ZON_ID
                            //join e in contexto.BAS_ZONAS_GEOGRAFICA1s on d.ZON_PADRE_ID equals e.ZON_ID

                                                   

                            select new
                            {
                                a.AGE_ID,
                                a.AGE_NOMBRES_APELLIDOS,
                                a.Departamento,
                                a.Municipio,
                                a.AREA,
                                a.AGE_CORREO_ELECTRONICO,
                                a.AGE_GENERO,
                                a.AGE_PAGINA_WEB,
                                a.AGE_ESTADO
                         
                            };
                if (datos == null) return;
                ASPxGridViewAgentes.BeginUpdate();
                ASPxGridViewAgentes.DataSource = datos;
                ASPxGridViewAgentes.DataBind();
                ASPxGridViewAgentes.EndUpdate();
                AgentesDanzaDataTableCache = GetDataView(ASPxGridViewAgentes);
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }
        #endregion

        #region btnNuevo_Click
        protected void btnNuevo_Click(object sender, EventArgs e)
        {

            Server.Transfer("AgentesAdministracion.aspx", true);
        }
        #endregion

        #region btnActualizar_Click
        protected void btnActualizar_Click(object sender, EventArgs e)
        {
            List<object> keyValues = ASPxGridViewAgentes.GetSelectedFieldValues("AGE_ID");

            try
            {
                foreach (object key in keyValues)
                {
                    StrAgeId = key.ToString();
                }
                crearContextItems();
                Server.Transfer("AgentesAdministracion.aspx", true);
                //Response.Redirect("AgrupacionesAdministracion.aspx", true);
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg1_error", "alert('" + ex.Message.ToString() + "');", true);
            }
        }
        #endregion

        #region Boton Publicar
        protected void btnPublicar_Click(object sender, EventArgs e)
        {
            try
            {
                Model.DataClassesAgentesDataContext contexto = new Model.DataClassesAgentesDataContext();

                var datos = from a in contexto.ART_AGENTEs
                            where a.AGE_ID == Convert.ToDecimal(StrAgeId)
                            select a;

                foreach (var v in datos)
                {

                    v.AGE_ESTADO = 'P';
                    contexto.SubmitChanges();

                }
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg1_error", "LA INFORMACIÓN SE PUBLICÓ EXITOSAMENTE", true);
            }
            catch (Exception ex)
            {

                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "msg1_error", ex.Message.ToString(), true);
            }

        }
        #endregion

    }
}